package com.neuedu.his.controller;

import com.neuedu.his.mapper.CheckapplyMapper;
import com.neuedu.his.po.Checkapply;
import com.neuedu.his.service.CheckapplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/check")
public class CheckApplyController {

    @Autowired
    CheckapplyService service;


    @RequestMapping("/getbymid")
    public Map<String, Object> getbymid(@RequestParam Integer mid) {
        try {
            return service.getinfoAndPriceByMedicalID(mid);
        } catch (Exception e) {
            e.printStackTrace();  // 打印详细错误
            Map<String, Object> map = new HashMap<>();
            map.put("error", "系统异常：" + e.getMessage());
            return map;
        }
    }

    // 收费接口，接收 { ids: [1,2,3] } 形式的请求体
    @PostMapping("/charge")
    public ResultWrapper charge(@RequestBody Map<String, List<Integer>> param) {
        List<Integer> ids = param.get("ids");
        if (ids == null || ids.isEmpty()) {
            return ResultWrapper.fail("收费ID列表不能为空");
        }
        try {
            boolean success = service.chargeByIds(ids);
            if (success) {
                return ResultWrapper.success("收费成功");
            } else {
                return ResultWrapper.fail("收费失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultWrapper.fail("收费异常：" + e.getMessage());
        }
    }

    // 执行确认
    @PostMapping("/execute")
    public ResultWrapper execute(@RequestBody Map<String, List<Integer>> map) {
        List<Integer> ids = map.get("ids");
        boolean result = service.batchExecute(ids);
        return result ? ResultWrapper.success("执行成功") : ResultWrapper.fail("执行失败");
    }

    // 取消执行
    @PostMapping("/cancelExecute")
    public ResultWrapper cancelExecute(@RequestBody Map<String, List<Integer>> map) {
        List<Integer> ids = map.get("ids");
        boolean result = service.batchCancelExecute(ids);
        return result ? ResultWrapper.success("取消执行成功") : ResultWrapper.fail("取消执行失败");
    }

    // 结果录入
    @PostMapping("/inputResult")
    public ResultWrapper inputResult(@RequestParam Integer id,
                                     @RequestParam String result,
                                     @RequestParam Integer resultOperID) {
        boolean res = service.inputResult(id, result, resultOperID);
        return res ? ResultWrapper.success("结果录入成功") : ResultWrapper.fail("录入失败");
    }




}